<template>
  <div class="channel-edit">
    <van-cell>
      <div
        slot="title"
        class="title-text"
      >我的频道</div>
      <van-button
        class="edit-btn"
        size="mini"
        round
        plain
        @click="isEdit=!isEdit"
      >{{isEdit?'完成':'编辑'}}</van-button>
    </van-cell>
    <van-grid
      class='my-grid'
      :gutter="10"
    >
      <van-grid-item
        class="grid-item"
        v-for="(value,index) in myChannels"
        :key="index"
        @click="onMyChannelClick(value,index)"
      >
        <van-icon
          v-show="isEdit&&!fixedChannels.includes(value.id)"
          slot="icon"
          name="clear"
        ></van-icon>
        <span
          :class="{ active:index===active }"
          class="text"
          slot="text"
        >{{value.name}}</span>
      </van-grid-item>
    </van-grid>
    <van-cell>
      <div slot="title">频道推荐</div>
    </van-cell>
    <van-grid
      class='recommend-grid'
      :gutter="10"
    >
      <van-grid-item
        class="grid-item"
        v-for="value in recommendChannel"
        :key="value.id"
        icon='plus'
        :text="value.name"
        @click="onAddChannel(value)"
      />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, setUserChannels, deleteUserChannels } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    myChannels: {
      type: Array,
      requried: true
    },
    active: {
      type: Number,
      requried: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false,
      fixedChannels: [0]
    }
  },
  // 计算属性
  computed: {
    ...mapState(['user']),
    recommendChannel () {
      // return this.allChannels.filter((allChannel) => {
      //   return !this.myChannels.find((myChannel) => {
      //     return allChannel.id === myChannel.id
      //   })
      // })
      const arr = []
      this.allChannels.forEach((allChannel) => {
        const res = this.myChannels.find((myChannel) => {
          return allChannel.id === myChannel.id
        })
        if (!res) {
          arr.push(allChannel)
        }
      })
      return arr
    }
  },
  // 侦听器
  watch: {},
  created () {
    this.loadAllChannels()
  },
  mounted () { },
  methods: {
    async loadAllChannels () {
      try {
        const data = await getAllChannels()
        this.allChannels = data.data.data.channels
        // console.log(data.data.data.channels)
        this.$nextTick(() => {
          console.log(this.allChannels)
        })
      } catch (err) {
        this.$toast.fail('失败文案')
      }
    },
    async onAddChannel (value) {
      this.$emit('pushChannel', value)
      // this.myChannels.push(value)
      if (this.user) {
        try {
          // 已登录，数据存储到线上
          await setUserChannels({
            id: value.id, // 频道 id
            seq: this.myChannels.length // 频道的 序号
          })
          this.$toast('添加成功')
        } catch (err) {
          this.$toast('保存失败')
        }
      } else {
        // 未登陆
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    onMyChannelClick (channel, index) {
      if (!this.isEdit) {
        this.$emit('update-active', index, false)
      } else {
        if (this.fixedChannels.includes(channel.id)) {
          return
        }
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        this.$emit('deleteChannel', index)
        // this.myChannels.splice(index, 1)
        this.deleteHandle(channel)
      }
    },
    async deleteHandle (channel) {
      if (this.user) {
        try {
          await deleteUserChannels(channel.id)
          this.$toast('移出成功')
        } catch (err) {
          this.$toast('移出失败')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }
  .edit-btn {
    width: 104px;
    height: 48;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text {
        font-size: 28px;
        color: #222;
      }
    }
  }
  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
        .van-grid-item__text {
          margin-top: 0;
        }
      }
    }
  }
}
</style>
